﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using Autofac;
using Autofac.Integration.Mvc;
using System.Reflection;
using MI.ORM;
using NHibernate;
using MI.ORM.IDAL;
using MI.ORM.DAL;
using System.Web.Mvc;
using MI.ORM.Repository;
using MI.Service;

namespace MI.Web.IoC
{
    public static class Bootstrapper
    {
        public static void Run()
        {
            var builder = new ContainerBuilder();
            builder.RegisterControllers(Assembly.GetExecutingAssembly());

            FluentNhibernateHelper helper = new FluentNhibernateHelper();
            builder.RegisterInstance(helper.SessionFactory).As<ISessionFactory>().SingleInstance();
            builder.RegisterType<Database>().As<IDatabase>().InstancePerLifetimeScope();
            builder.RegisterType<UnitOfWork>().As<MI.ORM.IDAL.IUnitOfWork>().InstancePerLifetimeScope();
            builder.RegisterGeneric(typeof(Repository<>)).As(typeof(IRepository<>)).InstancePerLifetimeScope();
            builder.RegisterAssemblyTypes(typeof(CategoryService).Assembly)
                .Where(t => t.Name.EndsWith("Service"))
                .AsImplementedInterfaces().InstancePerLifetimeScope();

            IContainer container = builder.Build();
            DependencyResolver.SetResolver(new AutofacDependencyResolver(container));
        }
    }
}